<template>
  <div class="message-container">
    <!-- 搜索区域 -->
    <ContentWrap class="search-wrapper">
      <!-- 搜索头部 -->
      <div class="search-header">
        <div class="header-icon">
          <Icon icon="ep:chat-dot-round" />
        </div>
        <div class="header-content">
          <h3 class="search-title">对话消息管理</h3>
          <p class="search-subtitle">管理和查看所有对话消息记录</p>
        </div>
      </div>

      <!-- 搜索表单 -->
      <div class="search-form-container">
        <el-form
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="100px"
          label-position="left"
          class="enhanced-search-form"
        >
          <div class="form-grid">
            <el-form-item label="消息内容" prop="msgContent" class="form-item">
              <el-input
                v-model="queryParams.msgContent"
                placeholder="请输入消息内容"
                clearable
                @keyup.enter="handleQuery"
                class="enhanced-input"
              >
                <template #prefix>
                  <Icon icon="ep:chat-line-round" />
                </template>
              </el-input>
            </el-form-item>

            <el-form-item label="创建时间" prop="createTime" class="form-item">
              <el-date-picker
                v-model="queryParams.createTime"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
                class="enhanced-date-picker"
              />
            </el-form-item>

            <el-form-item label="消息类型" prop="msgType" class="form-item">
              <el-select
                v-model="queryParams.msgType"
                placeholder="请选择消息类型"
                clearable
                class="enhanced-select"
              >
                <template #prefix>
                  <Icon icon="ep:collection-tag" />
                </template>
                <el-option
                  v-for="(item, index) in msgTypeEnum"
                  :key="index"
                  :label="item.text"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </div>

          <div class="form-actions">
            <el-button @click="handleQuery" class="search-btn">
              <Icon icon="ep:search" class="btn-icon" />
              搜索
            </el-button>
            <el-button @click="resetQuery" class="reset-btn">
              <Icon icon="ep:refresh" class="btn-icon" />
              重置
            </el-button>
            <el-button
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['service:message:export']"
              class="export-btn"
            >
              <Icon icon="ep:download" class="btn-icon" />
              导出
            </el-button>
          </div>
        </el-form>
      </div>
    </ContentWrap>

    <!-- 列表区域 -->
    <ContentWrap class="table-wrapper">
      <!-- 表格 -->
      <div class="table-container">
        <el-table
          v-loading="loading"
          :data="list"
          :stripe="true"
          :show-overflow-tooltip="true"
          border
          class="enhanced-table"
        >
          <el-table-column label="发件人" align="center" width="120">
            <template #default="scope">
              <div class="sender-info">
                <el-tag v-if="scope.row.userType == 1" class="sender-tag user-tag" type="primary">
                  <Icon icon="ep:user" class="tag-icon" />
                  用户
                </el-tag>
                <el-tag v-else class="sender-tag service-tag" type="success">
                  <Icon icon="ep:service" class="tag-icon" />
                  客服
                </el-tag>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="消息类型" align="center" prop="msgType" width="140">
            <template #default="scope">
              <div class="message-type">
                <el-tag
                  v-if="msgType[scope.row.msgType]"
                  :type="msgType[scope.row.msgType].type"
                  class="type-tag"
                >
                  <Icon :icon="getTypeIcon(scope.row.msgType)" class="tag-icon" />
                  {{ msgType[scope.row.msgType].text }}
                </el-tag>
                <span v-else class="no-data">未知类型</span>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="消息内容" align="left" prop="msgContent" min-width="300">
            <template #default="scope">
              <div class="message-content">
                <div class="content-wrapper">
                  <Icon icon="ep:chat-dot-round" class="content-icon" />
                  <div class="content-text" v-html="scope.row.msgContent"></div>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="创建时间"
            :formatter="dateFormatter"
            align="center"
            prop="createTime"
            width="180"
          >
            <template #default="scope">
              <div class="time-info">
                <Icon icon="ep:clock" class="time-icon" />
                <span class="time-text">{{
                  dateFormatter(scope.row, null, scope.row.createTime)
                }}</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </ContentWrap>
  </div>
</template>

<script setup lang="ts">
import * as MessageApi from '@/api/service/message'
import { dateFormatter } from '@/utils/formatTime'

defineOptions({ name: 'ServiceMessage' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  sessionId: null,
  msgType: null,
  msgContent: null,
  customerId: null,
  customerName: null,
  assistantId: null,
  assistantName: null,
  assistantAvator: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MessageApi.getMessagePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const msgType = {
  1: { text: '上线消息', type: '' },
  2: { text: '离线消息', type: 'danger' },
  3: { text: '对话消息', type: 'success' },
  4: { text: '欢迎语', type: 'info' }
}

const msgTypeEnum = ref([
  { text: '上线', value: 1, id: 1 },
  { text: '离线', value: 2, id: 2 },
  { text: '普通消息', value: 3, id: 3 },
  { text: '欢迎语', value: 4, id: 4 },
  { text: '场景标签列表', value: 5, id: 5 },
  { text: '发标签消息', value: 6, id: 6 },
  { text: '表单消息', value: 7, id: 7 }
])

// 获取消息类型图标
const getTypeIcon = (type: number) => {
  const iconMap = {
    1: 'ep:connection',
    2: 'ep:switch-button',
    3: 'ep:chat-dot-round',
    4: 'ep:star',
    5: 'ep:collection-tag',
    6: 'ep:price-tag',
    7: 'ep:document'
  }
  return iconMap[type] || 'ep:chat-line-round'
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MessageApi.deleteMessage(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MessageApi.exportMessage(queryParams)
    download.excel(data, '对话消息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$info-color: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.message-container {
  padding: 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;

  @media (max-width: 768px) {
    padding: 8px;
  }
}

/* 搜索区域样式 */
.search-wrapper {
  margin-bottom: 16px;
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
}

.search-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  border-radius: 10px;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 12px;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
  margin-left: 16px;

  @media (max-width: 768px) {
    margin-left: 0;
    text-align: center;
  }
}

.search-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.search-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

.search-form-container {
  background: white;
  padding: 24px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-search-form {
  .form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-bottom: 20px;

    @media (max-width: 768px) {
      grid-template-columns: 1fr;
      gap: 16px;
    }
  }

  .form-item {
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: $text-regular;
    }
  }
}

.enhanced-input,
.enhanced-select,
.enhanced-date-picker {
  width: 100%;

  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

.form-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;

  @media (max-width: 768px) {
    flex-direction: column;
  }
}

.search-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  color: white;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.reset-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.export-btn {
  border: 2px solid $primary-color;
  color: $primary-color;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    background: $primary-light;
    transform: translateY(-1px);
  }

  @media (max-width: 768px) {
    width: 100%;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 表格区域样式 */
.table-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
}

.table-container {
  background: white;
  overflow-x: auto;
}

.enhanced-table {
  :deep(.el-table__header) {
    background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%);

    th {
      background: transparent !important;
      color: $text-primary;
      font-weight: 600;
      font-size: 14px;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
        color: $text-regular;
        font-size: 13px;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格内容样式 */
.sender-info {
  display: flex;
  justify-content: center;
  align-items: center;
}

.sender-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 6px;
  font-weight: 500;
  font-size: 12px;
  padding: 4px 8px;

  .tag-icon {
    font-size: 12px;
  }

  &.user-tag {
    background: rgba(0, 82, 204, 0.1);
    border-color: rgba(0, 82, 204, 0.3);
    color: $primary-color;
  }

  &.service-tag {
    background: rgba(103, 194, 58, 0.1);
    border-color: rgba(103, 194, 58, 0.3);
    color: $success-color;
  }
}

.message-type {
  display: flex;
  justify-content: center;
  align-items: center;
}

.type-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 6px;
  font-weight: 500;
  font-size: 12px;
  padding: 4px 8px;

  .tag-icon {
    font-size: 12px;
  }

  &.el-tag {
    border-radius: 6px;
  }

  &.el-tag--success {
    background: rgba(103, 194, 58, 0.1);
    border-color: rgba(103, 194, 58, 0.3);
    color: $success-color;
  }

  &.el-tag--danger {
    background: rgba(245, 108, 108, 0.1);
    border-color: rgba(245, 108, 108, 0.3);
    color: $danger-color;
  }

  &.el-tag--info {
    background: rgba(144, 147, 153, 0.1);
    border-color: rgba(144, 147, 153, 0.3);
    color: $info-color;
  }

  &.el-tag--primary {
    background: rgba(0, 82, 204, 0.1);
    border-color: rgba(0, 82, 204, 0.3);
    color: $primary-color;
  }
}

.message-content {
  padding: 8px 0;
}

.content-wrapper {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  line-height: 1.5;
}

.content-icon {
  color: $primary-color;
  font-size: 16px;
  flex-shrink: 0;
  margin-top: 2px;
}

.content-text {
  color: $text-primary;
  font-size: 13px;
  word-break: break-word;
  line-height: 1.6;
  flex: 1;

  // 处理HTML内容样式
  :deep(p) {
    margin: 0;
    line-height: 1.6;
  }

  :deep(img) {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
    margin: 4px 0;
  }

  :deep(a) {
    color: $primary-color;
    text-decoration: none;

    &:hover {
      text-decoration: underline;
    }
  }
}

.time-info {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;
  font-size: 12px;
  color: $text-secondary;
}

.time-icon {
  color: $text-secondary;
  font-size: 14px;
}

.time-text {
  font-weight: 500;
}

.no-data {
  color: $text-placeholder;
  font-size: 12px;
  text-align: center;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .message-container {
    padding: 4px;
  }

  .search-form-container {
    padding: 12px;
  }

  .enhanced-search-form {
    .form-grid {
      gap: 12px;
    }

    .form-item {
      :deep(.el-form-item__label) {
        width: 80px !important;
        font-size: 13px;
      }
    }
  }

  .form-actions {
    gap: 8px;

    .el-button {
      padding: 8px 12px;
      font-size: 13px;
    }
  }

  .enhanced-table {
    :deep(.el-table__header) th {
      padding: 10px 6px;
      font-size: 12px;
    }

    :deep(.el-table__body) td {
      padding: 10px 6px;
      font-size: 11px;
    }
  }

  .sender-tag,
  .type-tag {
    font-size: 11px;
    padding: 3px 6px;
  }

  .content-text {
    font-size: 12px;
  }

  .time-info {
    font-size: 11px;
  }

  .pagination-wrapper {
    padding: 12px;
  }
}

/* 滚动条美化 */
:deep(.el-scrollbar__bar) {
  &.is-vertical .el-scrollbar__thumb {
    background: rgba(0, 82, 204, 0.3);
    border-radius: 4px;

    &:hover {
      background: rgba(0, 82, 204, 0.5);
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .message-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .search-wrapper,
  .table-wrapper {
    background: #374151;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .search-form-container,
  .table-container,
  .pagination-wrapper {
    background: #374151;
  }

  .search-title {
    color: #f9fafb;
  }

  .content-text,
  .time-text {
    color: #f9fafb;
  }

  .content-icon,
  .time-icon {
    color: #d1d5db;
  }

  .enhanced-table {
    :deep(.el-table__header) {
      background: #4b5563;

      th {
        background: #4b5563 !important;
        color: #f9fafb;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }
}
</style>
